<?php

namespace iBrand\Store\Backend\Http\Controllers;

use iBrand\Backend\Http\Controllers\Controller;
use iBrand\Store\Backend\Model\Goods;
use iBrand\Store\Backend\Model\Supplier;
use Illuminate\Http\Request;
use Validator;
use Encore\Admin\Facades\Admin as LaravelAdmin;
use Encore\Admin\Layout\Content;

class SupplierController extends Controller
{

	public function index()
	{
		$suppliers = Supplier::paginate(10);

		return LaravelAdmin::content(function (Content $content) use ($suppliers) {

			$content->header('供应商管理');

			$content->breadcrumb(
				['text' => '供应商管理', 'url' => 'store/supplier', 'no-pjax' => 1],
				['text' => '供应商列表', 'url' => '', 'no-pjax' => 1, 'left-menu-active' => '供应商列表']

			);

			$content->body(view('store-backend::supplier.index', compact('suppliers')));
		});
	}

	public function create()
	{
		return LaravelAdmin::content(function (Content $content) {

			$content->header('供应商管理');

			$content->breadcrumb(
				['text' => '供应商管理', 'url' => 'store/supplier', 'no-pjax' => 1],
				['text' => '创建供应商', 'url' => '', 'no-pjax' => 1, 'left-menu-active' => '供应商列表']

			);

			$content->body(view('store-backend::supplier.create'));
		});
	}

	public function store(Request $request)
	{
		$input = $request->except('id', '_token', 'file');
		if (!$input['name'] OR !$input['code']) {
			return $this->ajaxJson(false, [], 200, '请完善供应商信息');
		}

		$prefix = config('ibrand.app.database.prefix', 'ibrand_');
		$rules  = [
			'name' => 'required',
			'code' => 'required|unique:' . $prefix . 'base_supplier,code',
		];
		if ($id = request('id')) {
			$rules['code'] = 'required|unique:el_supplier,code,' . $id;
		}

		$message = [
			"required" => ":attribute 不能为空",
			"unique"   => ":attribute 已存在",
		];

		$attributes = [
			"name" => '供应商名称',
			"code" => '编码',
		];

		$validator = Validator::make(
			$request->all(),
			$rules,
			$message,
			$attributes
		);
		if ($validator->fails()) {
			$warnings     = $validator->messages();
			$show_warning = $warnings->first();

			return $this->ajaxJson(false, [], 300, $show_warning);
		}

		if ($id = request('id')) {
			$supplier                  = Supplier::find($id);
			$supplier->name            = $input['name'];
			$supplier->code            = $input['code'];
			$supplier->status          = $input['status'];
			$supplier->company_name    = $input['company_name'];
			$supplier->company_number  = $input['company_number'];
			$supplier->company_address = $input['company_address'];
			$supplier->company_phone   = $input['company_phone'];
			$supplier->company_limit   = $input['company_limit'];
			$supplier->company_scope   = $input['company_scope'];
			$supplier->company_license = $input['company_license'];
			$supplier->company_permit  = $input['company_permit'];
			$supplier->save();

			return $this->ajaxJson();
		} else {
			if (Supplier::create($input)) {
				return $this->ajaxJson();
			}
		}

		return $this->ajaxJson(false, [], 300, '保存失败');
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int $id
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function edit($id)
	{
		if ($id == 1) {
			return redirect(route('admin.supplier.index'));
		}
		$supplier = Supplier::find($id);

		return LaravelAdmin::content(function (Content $content) use ($supplier) {

			$content->header('编辑供应商');

			$content->breadcrumb(
				['text' => '供应商管理', 'url' => 'store/supplier', 'no-pjax' => 1],
				['text' => '编辑供应商', 'url' => '', 'no-pjax' => 1, 'left-menu-active' => '供应商列表']

			);

			$content->body(view('store-backend::supplier.edit', compact('supplier')));
		});
//        return view('store-backend::supplier.edit', compact('supplier'));
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int $id
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function delete($id)
	{
		if (Goods::where('supplier_id', $id)->first()) {
			return $this->ajaxJson(false, [], 404, '已有商品关联该供应商，不能删除');
		}

		Supplier::destroy($id);

		return $this->ajaxJson();
	}
}
